# Maintainer: Davorin Učakar <davorin.ucakar@gmail.com>

pkgbase=openzone
pkgname=('liboz' 'openzone' 'openzone-data')
pkgver=0.3.95
pkgrel=1
url="http://ducakar.github.com/openzone/"
license=('GPL3')
arch=('i686' 'x86_64')
makedepends=('cmake' 'gcc-libs' 'physfs' 'lua' 'mesa' 'sdl2_ttf' 'libjpeg' 'libvorbis' 'openal')
source=("https://github.com/downloads/ducakar/openzone/${pkgbase}-src-${pkgver}.tar.xz"
        "https://github.com/downloads/ducakar/openzone/${pkgbase}-data-${pkgver}.tar.xz")
sha256sums=('SKIP'
            'SKIP')
noextract=("${pkgbase}-src-${pkgver}.tar.xz"
           "${pkgbase}-data-${pkgver}.tar.xz")

function prepare()
{
  tar xf ${pkgbase}-src-${pkgver}.tar.xz
  tar xf ${pkgbase}-data-${pkgver}.tar.xz
}

function build()
{
  cd "${srcdir}/${pkgbase}-${pkgver}"
  mkdir -p build && cd build

  cmake \
    -D CMAKE_INSTALL_PREFIX=/usr \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_AR="/usr/bin/gcc-ar" \
    -D CMAKE_RANLIB="/usr/bin/gcc-ranlib" \
    -D CMAKE_CXX_FLAGS="${CXXFLAGS/-O2} -msse3 -mfpmath=sse" \
    -D CMAKE_CXX_FLAGS_RELEASE="-Ofast -flto -fprofile-generate" \
    -D BUILD_SHARED_LIBS=1 \
    -D OZ_NONFREE=1 \
    ..

  make

  ./src/tools/openzone -l

  cmake \
    -D CMAKE_CXX_FLAGS_RELEASE="-Ofast -flto -fprofile-use" \
    ..

  make
}

function package_liboz()
{
  pkgdesc='OpenZone ozCore, ozEngine and ozFactory libraries'
  license=('ZLIB')
  depends=('gcc-libs' 'physfs' 'libgl' 'openal' 'libjpeg' 'libvorbis' 'sdl2_ttf')

  cd "${srcdir}/${pkgbase}-${pkgver}"

  ( cd build && make install DESTDIR="${pkgdir}" )

  rm -rf "${pkgdir}"/usr/{bin,share}

  install -Dm644 "src/ozCore/COPYING" "${pkgdir}/usr/share/licenses/liboz/COPYING"
}

function package_openzone()
{
  pkgdesc='Simple cross-platform FPS/RTS game engine'
  depends=('liboz' 'lua' 'openzone-data')
  optdepends=('espeak: speech synthesis'
              'libmad: MP3 streaming'
              'faad2: AAC streaming'
              'zip: building ZIP game data archives'
              'p7zip: building 7zip game data archives')

  cd "${srcdir}/${pkgbase}-${pkgver}"

  ( cd build && make install DESTDIR="${pkgdir}" )

  rm -rf "${pkgdir}/usr/"{lib,include}

  install -dm755 "${pkgdir}/usr/share/doc/${pkgname}-${pkgver}/licences"
  install -m644 AUTHORS ChangeLog.md README.md TODO.md doc/*.html \
                "${pkgdir}/usr/share/doc/${pkgname}-${pkgver}"
  install -m644 doc/licences/* "${pkgdir}/usr/share/doc/${pkgname}-${pkgver}/licences"
}

function package_openzone-data()
{
  pkgdesc='OpenZone game data'
  license=('CCPL:by-nc-nd')
  arch=('any')

  cd "${srcdir}/${pkgbase}-${pkgver}"

  install -dm755 "${pkgdir}/usr/share/openzone"
  install -m644 "share/openzone/"*.zip "${pkgdir}/usr/share/openzone"
}
